/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.rie06.rmi;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 *
 * @author clement
 */
public class ProcessusImpl extends UnicastRemoteObject implements Processus {

    private List<Proc> processus_list;
    
    public ProcessusImpl() throws RemoteException {
        processus_list = new ArrayList<Proc>();
    }

    public List<Proc> getListOfProcessus() throws RemoteException {
        update();
        return processus_list;
    }

    private void update() throws RemoteException {
        try {
            processus_list.clear();
            String[] args = {"/usr/bin/top", "-b", "-n", "1"};
            final Process process = Runtime.getRuntime().exec(args);

            /*
             * Traitement du flux de sortie
             */

            BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
            String line = "";
            try {
                while ((line = reader.readLine()) != null) {
                    String[] tokens = line.split(" ");
                    String pid = null;
                    String user = null;
                    String priorite = null;
                    String cpu = null;
                    String mem = null;
                    String command = null;
                    int trouve = 0;
                    boolean error = false;
                    for (int i = 0; i < tokens.length; i++) {
                        if (!tokens[i].isEmpty()) {
                            trouve++;
                            //test si le 1er contient le PID, sinon on passe au suivant
                            if (trouve == 1) {
                                Matcher matcher = Pattern.compile("\\d+").matcher(tokens[i]);
                                if (!matcher.find()) {
                                    error = true;
                                    System.out.println("*** Ignore line start with: " + tokens[i]);
                                }
                            }
                            if (!error) {
                                if (trouve == 1) {
                                    pid = tokens[i];
                                }
                                if (trouve == 2) {
                                    user = tokens[i];
                                }
                                if (trouve == 3) {
                                    priorite = tokens[i];
                                }
                                if (trouve == 9) {
                                    cpu = tokens[i];
                                }
                                if (trouve == 10) {
                                    mem = tokens[i];
                                }
                                if (trouve == 12) {
                                    command = tokens[i];
                                }
                            }
                        }
                    }
                    if (pid != null) {
                        System.out.println("====Debug line====");
                        System.out.println("pid: " + pid);
                        System.out.println("user: " + user);
                        System.out.println("priorite: " + priorite);
                        System.out.println("cpu: " + cpu);
                        System.out.println("mem: " + mem);
                        System.out.println("command: " + command);
                        System.out.println("==================");
                        Proc proc = new Proc(pid, user, priorite, cpu, mem, command);
                        processus_list.add(proc);
                    }

                }

                reader.close();

            } catch (IOException ioe) {
            }
        } catch (IOException ex) {
            Logger.getLogger(MemoryImpl.class.getName()).log(Level.SEVERE, null, ex);
            throw new UnsupportedOperationException("Error with parsing processus....");
        }
    }
}
